package cn.tedu.note.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import cn.tedu.note.dao.NoteDao;
import cn.tedu.note.dao.NotebookDao;
import cn.tedu.note.dao.UserDao;
import cn.tedu.note.entity.Note;
import cn.tedu.note.entity.Notebook;
import cn.tedu.note.entity.User;

@Service("noteService")
public class NoteServiceImpl 
	implements NoteService {
	
	@Resource
	private NoteDao noteDao;
	
	@Resource 
	private NotebookDao notebookDao;
	
	@Resource 
	private UserDao userDao;
	
	@Transactional
	public List<Map<String, Object>> 
		listNotes(String notebookId) 
		throws NotebookNotFoundException {
		if(notebookId==null || notebookId.trim().isEmpty()){
			throw new NotebookNotFoundException("Id不能空");
		}
		Notebook notebook=notebookDao
			.findNotebookById(notebookId);
		// countNotebookById(notebookId)
		if(notebook==null){
			throw new NotebookNotFoundException("笔记本不存在");
		}
		return noteDao.findNotesByNotebookId(notebookId);
	}
	
	@Transactional(readOnly=true)
	public Note getNote(String id) 
		throws NoteNotFoundException {
		if(id==null || id.trim().isEmpty()){
			throw new NoteNotFoundException("ID空");
		}
		Note note = noteDao.findNoteById(id);
		if(note==null){
			throw new NoteNotFoundException("ID错误");
		}
		return note;
	}
	
	@Transactional
	public boolean saveNote(
			String id, 
			String title, 
			String body) 
		throws NoteNotFoundException {
		if(id==null||id.trim().isEmpty()){
			throw new NoteNotFoundException("id空");
		}
		if(body==null){
			body="";
		}
		if(title==null || 
				title.trim().isEmpty()){
			//截取body中的数据
			String reg="<p>[^<>]+<\\/p>";
			Pattern p=Pattern.compile(reg);
			Matcher m = p.matcher(body);
			if(m.find()){
				String str = m.group();
				title=str.substring(
					3, str.length()>13?
					13:str.length()-4).trim();
			}else{					
				//获取Body数据
				title="无标题";
			}
		}

		Map<String, Object> note=
			new HashMap<String, Object>();
		note.put("id", id);
		note.put("title", title);
		note.put("body", body);
		note.put("lastModifyTime", 
			System.currentTimeMillis());
		int n = noteDao.updateNote(note);
		return n==1;
	}
	
	@Transactional
	public Note addNote(
			String notebookId, 
			String userId, String title) 
		throws NotebookNotFoundException,
		UserNotFoundException{

		Thread t = Thread.currentThread();
		System.out.println(t.getName());

		if(notebookId==null||notebookId.trim().isEmpty()){
			throw new NotebookNotFoundException("笔记本ID为空"); 
		}
		if(userId==null||userId.trim().isEmpty()){
			throw new UserNotFoundException("用户ID为空");
		}
		if(title==null||title.trim().isEmpty()){
			title = "无标题";
		}
		User user = userDao.findUserById(userId);
		if(user==null){
			throw new UserNotFoundException("没有用户");
		}
		Notebook notebook = notebookDao.findNotebookById(notebookId);
		if(notebook==null){
			throw new NotebookNotFoundException("笔记本不存在");
		}
		String id = UUID.randomUUID().toString();
		long now = System.currentTimeMillis();
		String statusId="1";
		String typeId="1";
		String body = "";
		Note note=new Note(id, notebookId, userId, statusId, typeId, title, body, now, now);
		int n = noteDao.addNote(note);
		if(n!=1){
			throw new RuntimeException("保存失败!");
		}
		return note;
	}
	
	@Transactional
	public int deleteNotes(String... ary) {
		int n=0;
		for(String id: ary){
			Note note=noteDao.findNoteById(id);
			if(note==null){
				throw new NoteNotFoundException("id:"+id);
			}
			n+=noteDao.deleteNoteById(id);
		}
		return n;
	}
}




